//
//  SEGOrderTrackView.m
//  Uhomebk
//
//  Created by mac on 2017/10/25.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "SEGOrderTrackView.h"
#import "SEGTrackTextView.h"
#import "SEGTrackPhotoView.h"
#import "SEGTrackRecordView.h"
#import "SEGOrderTasksView.h"
#import "SEGTrackPayView.h"
#import "SEGTrackMaterelView.h"
//#import "YYText.h"
#import "SEGTrackPhotoView.h"
#import "SEGTrackFileView.h"

#define kVlineViewX 69.5

#define kVlineViewWidth 1
@interface SEGOrderTrackView()
{
    UIViewController *_parentVC;
    UIView *_taskBgView;
    
    NSString *_nextUserName;
    NSString *_currUserName;
    NSString *_currFirstUserName;
    NSString *_nextFirstUserName;
    CGFloat _trackDescWidth;
    BOOL isOpen;
}

@end
@implementation SEGOrderTrackView
- (instancetype)initWithFrame:(CGRect)frame trackModel:(SEGOrderTrackInfoModel *)trackModel parentVC:(UIViewController *)parentVC isFirstTrack:(BOOL)isFirstTrack {
    if (self = [super initWithFrame:frame]) {
        _parentVC = parentVC;
        _isFirstTrack = isFirstTrack;
        [self setupUIWithFrame:frame trackModel:trackModel];
    }
    
    return self;
}


/**
 刷新视图

 @param trackModel
 */
-(void)refreshSubViewsWith:(SEGOrderTrackInfoModel *)trackModel
{
    //先清空视图
//    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//            [obj removeFromSuperview];
//    }];
    self.backgroundColor = kColor_W;
    CGFloat _trackViewHeight = 0;
    
    //1.布局工单轨迹描述信息视图
    [self setUpTrackInfoViewWith:trackModel];
    
    //2.布局工单轨迹提单信息视图
    [self setUpTrackDetailInfoWith:trackModel];
    
    //3.布局工单轨迹子任务视图
    [self setUpTrackTasksInfoWith:trackModel];
    
    //4.布局其他信息 - 有偿服务，物料清单，协同人信息
    [self setUpTrackOtherInfoWith:trackModel];
    
    //5.底部分割线
    [self setUpBottomLineView];
    
    self.height = _trackViewHeight+20;
}

/**
 UI布局
 */
-(void)setupUIWithFrame:(CGRect)frame trackModel:(SEGOrderTrackInfoModel *)trackModel
{
    self.backgroundColor = kColor_W;
    CGFloat _trackViewHeight = 0;
    
    //1.布局工单轨迹描述信息视图
    [self setUpTrackInfoViewWith:trackModel];
    
    //2.布局工单轨迹提单信息视图
    [self setUpTrackDetailInfoWith:trackModel];
    
    //3.布局工单轨迹子任务视图
     [self setUpTrackTasksInfoWith:trackModel];
    
    //4.布局其他信息 - 有偿服务，物料清单，协同人信息
    [self setUpTrackOtherInfoWith:trackModel];
    
    //5.底部分割线
    [self setUpBottomLineView];
   
    self.height = _trackViewHeight+20;
    
}

/**
 布局工单轨迹描述信息视图
 */
-(void)setUpTrackInfoViewWith:(SEGOrderTrackInfoModel *)trackModel
{
    
    UIView *trackInfoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 0)];
    trackInfoView.backgroundColor = kColor_W;
    trackInfoView.tag = 99999;
    [self addSubview:trackInfoView];
    //2.1节点描述信息
    /* 节点名字*/
    UILabel *trackNameIcon = [[UILabel alloc] initWithFrame:CGRectMake(kVlineViewX+15, 20, 33, 15)];
    trackNameIcon.font = kFont_11;
    [trackNameIcon setTextColor:kColor_W];
    trackNameIcon.backgroundColor = kColor_T;
    trackNameIcon.layer.cornerRadius = 7.5;
    trackNameIcon.layer.masksToBounds = YES;
    trackNameIcon.textAlignment = NSTextAlignmentCenter;
    trackNameIcon.text = trackModel.trackName;
    [trackNameIcon sizeToFit];
    trackNameIcon.width = trackNameIcon.width+10;
    trackNameIcon.height = 15;
    if ([trackModel.trackStatus isEqualToString:@"0"] || [trackModel.isLast isEqualToString:@"1"]) {
        trackNameIcon.backgroundColor = kColor_T;
    } else {
        if (self.isFirstTrack) {
            trackNameIcon.backgroundColor = kColor_T;
        } else if ([trackModel.trackCode isEqualToString:@"NODE_DS_DEAL"]) {
            trackNameIcon.backgroundColor = kColor_T;
        } else {
            trackNameIcon.backgroundColor = kColor_bg;
            [trackNameIcon setTextColor:kColor_3];
        }
    }

    /* 竖线*/
    UIView *vLineView = [[UIView alloc] initWithFrame:CGRectMake(kVlineViewX, 0, 1, 0)];
    vLineView.backgroundColor = kColor_L2;
    /* 节点*/
    UIImageView *trackIcon = [[UIImageView alloc] initWithFrame: CGRectMake(kVlineViewX-4.5, CGRectGetMidY(trackNameIcon.frame)-5, 10, 10)];
    trackIcon.backgroundColor = kColor_W;
    UIView *point = [[UIView alloc] initWithFrame:CGRectMake(1, 1, 8, 8)];
    point.layer.cornerRadius = 4;
    point.layer.masksToBounds = YES;
    [trackIcon addSubview:point];
    if ([trackModel.isLast isEqualToString:@"1"] || self.isFirstTrack) {
        point.backgroundColor = kColor_T;
        //trackIcon.image = [UIImage imageWithContentsOfFile:SEGPicName(@"img_orderpoint_color1")];
    }
    else{
        point.backgroundColor = [UIColor colorWithHexString:@"F0ECE9"];
    }
    
    /* 环节时间*/
    UILabel *trackTimeLbl = [[UILabel alloc] initWithFrame:CGRectMake(5, CGRectGetMidY(trackIcon.frame)-7.5, 50, 15)];
    trackTimeLbl.text = [SEGUtils getTimeWithTimeStr:trackModel.createDate timeFormat:@"HH:mm"];
    trackTimeLbl.textAlignment = NSTextAlignmentRight;
    [trackTimeLbl setTextColor:kColor_1];
    trackTimeLbl.font = kFont_15;
    /* 环节日期*/
    UILabel *trackDateLbl = [[UILabel alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(trackTimeLbl.frame)+2, 40, 10)];
    trackDateLbl.text = [SEGUtils getTimeWithTimeStr:trackModel.createDate timeFormat:@"MM-dd"];
    trackDateLbl.textAlignment = NSTextAlignmentRight;
    trackDateLbl.font = kFont_10;
    /* 环节描述信息*/
   _trackDescWidth = self.frame.size.width-kVlineViewX-30;
    NSArray *strArr = [self configTrackDescInfoWith:trackModel];
    __block NSString *trackDescStr = strArr[0];
//    __block NSString *trackOpenStr = strArr[1];
    //重新设置描述文字
//    trackDescStr = [trackModel.trackDescIsOpen isEqualToString:@"1"]?trackOpenStr:trackDescStr;
    SEGLabel *trackDescLbl = [self addTextLabelWith:trackDescStr trackModel:trackModel];
    
    //设置描述文字y轴坐标
    trackDescLbl.y = CGRectGetMaxY(trackNameIcon.frame)+12;

    /*环节处理意见*/
    NSString *trackName = trackModel.trackName;//当前环节名称
    NSString *dealAdvise = trackModel.dealAdvise;//处理意见
    NSString *trackAdviseStr  = [NSString stringWithFormat:@"%@意见：%@",trackName,dealAdvise];
    CGFloat trackAdviseWidth = self.frame.size.width-kVlineViewX-30;
    CGSize trackAdviseStrSize = [trackAdviseStr getAttributionHeightWithlineSpace:6 font:kFont_15 width:trackAdviseWidth];
    UILabel *trackAdviseLbl = [[UILabel alloc] initWithFrame:CGRectMake(kVlineViewX+15, CGRectGetMaxY(trackDescLbl.frame)+12, trackAdviseWidth, trackAdviseStrSize.height)];
    trackAdviseLbl.numberOfLines = 0;
    trackAdviseLbl.textColor = kColor_1;
    [trackAdviseLbl extendAttributedString:trackAdviseStr lineSpace:6 font:kFont_15];
    /*
    //没有意见信息直接隐藏
    if (trackModel.dealAdvise.length == 0 ) {
        trackAdviseLbl.height = 0;
        trackAdviseLbl.y -= 12;
    }
     */

    // 【需求 7350】直接隐藏外层节点处理意见文案，展示子节点数据
    trackAdviseLbl.height = 0;
    trackAdviseLbl.y -= 12;

    //重新适配高度
    trackInfoView.height = CGRectGetMaxY(trackAdviseLbl.frame);
    _trackViewHeight = CGRectGetMaxY(trackAdviseLbl.frame);
    vLineView.height = _trackViewHeight;
    
    [trackDateLbl setTextColor:kColor_3];
    [trackInfoView addSubview:vLineView];
    [trackInfoView addSubview:trackNameIcon];
    [trackInfoView addSubview:trackIcon];
    [trackInfoView addSubview:trackTimeLbl];
    [trackInfoView addSubview:trackDateLbl];
    [trackInfoView addSubview:trackDescLbl];
    // [trackInfoView addSubview:trackAdviseLbl];
    trackAdviseLbl.clipsToBounds = YES;
}

/**
 添加富文本点击事件
 */
-(SEGLabel *)addTextLabelWith:(NSString *)textStr trackModel:(SEGOrderTrackInfoModel *)trackModel
{
   __block NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:textStr];
    NSLog(@"text = %@",text);
     text.yy_color = kColor_1;
    NSString *clickStr = [textStr substringWithRange:NSMakeRange(textStr.length-2, 2)];
    if ([clickStr isEqualToString:@"展开"] || [clickStr isEqualToString:@"收起"] ) {
//        text.yy_alignment = NSTextAlignmentJustified;
        [text yy_setTextHighlightRange:NSMakeRange(textStr.length-2, 2) color:kColor_S3 backgroundColor:kColor_S3 tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull clickText, NSRange range, CGRect rect) {
            
            NSString *text_new = [NSString stringWithFormat:@"%@",clickText];
            //刷新label
            if ([text_new containsString:@"展开"]) {
                trackModel.trackDescIsOpen = @"1";
                if (self.delegate && [self.delegate respondsToSelector:@selector(openUserNameRefreshUI)]) {
                    [self.delegate openUserNameRefreshUI];
                }
            }
            if ([text_new containsString:@"收起"]) {
                trackModel.trackDescIsOpen = @"0";
                if (self.delegate && [self.delegate respondsToSelector:@selector(openUserNameRefreshUI)]) {
                    [self.delegate openUserNameRefreshUI];
                }
            }
        }];
    }
    
    SEGLabel *textLabel = [SEGLabel new];
    text.yy_font = kFont_15;
   
    text.yy_lineSpacing = 6;
    textLabel.numberOfLines = 0;
    textLabel.attributedText = text;
    //设置换行方式
    NSMutableParagraphStyle  *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
    text.yy_paragraphStyle = paragraphStyle;
    
    //计算文本尺寸
    CGSize maxSize = CGSizeMake(_trackDescWidth, MAXFLOAT);
    SEGTextLayout *layout = [SEGTextLayout layoutWithContainerSize:maxSize text:text];
    textLabel.textLayout = layout;
    CGFloat introHeight = layout.textBoundingSize.height;
    textLabel.x = kVlineViewX + 15;
    textLabel.width = maxSize.width;
    textLabel.height = introHeight;

    
    return textLabel;
}

-(void)setUpBottomLineView
{
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0,_trackViewHeight,self.width,20)];
    lineView.backgroundColor = kColor_W;
    
    UIView *vLineView = [[UIView alloc] initWithFrame:CGRectMake(kVlineViewX, 0, 1, 20)];
    vLineView.backgroundColor = kColor_L2;
    
    UIView *hLineView = [[UIView alloc] initWithFrame:CGRectMake(kVlineViewX+15, 19.5, self.width-kVlineViewX+15, 0.5)];
    hLineView.backgroundColor = kColor_L2;
    
    [lineView addSubview:vLineView];
    [lineView addSubview:hLineView];
    [self addSubview:lineView];
    
    _trackViewHeight += 20;
    
}
-(void)setUpTrackOtherInfoWith:(SEGOrderTrackInfoModel *)trackModel
{
    UIView *otherBgView = [[UIView alloc] initWithFrame:CGRectMake(0, _trackViewHeight, self.width, 0)];
    [self addSubview:otherBgView];
   __block CGFloat otherBgViewHeight = 0;
    //有偿服务
    if (trackModel.payServices.count > 0) {
      
        SEGTrackPayView *serviceView = [[SEGTrackPayView alloc] initWithFrame:CGRectMake(0, otherBgViewHeight, otherBgView.width, 0) trackModel:trackModel];
        serviceView.parentVC = self.parentVC;
        otherBgViewHeight += serviceView.height;
        [otherBgView addSubview:serviceView];
    }
    //物料信息
     if (trackModel.payServices.count == 0 && trackModel.materiels.count > 0){
        SEGTrackMaterelView *materelView = [[SEGTrackMaterelView alloc] initWithFrame:CGRectMake(0, otherBgViewHeight, otherBgView.width, 0) trackModel:trackModel];
         materelView.parentVC = self.parentVC;
        otherBgViewHeight += materelView.height;
        [otherBgView addSubview:materelView];
    }
    
    //重新布局其他信息视图和轨迹视图高度
    otherBgView.height = otherBgViewHeight;
    _trackViewHeight += otherBgViewHeight;
    NSLog(@" otherBgViewHeight = %.f",otherBgViewHeight);
}

-(void)setUpTrackTasksInfoWith:(SEGOrderTrackInfoModel *)trackModel
{
    __block CGFloat taskBgViewHeight = 0;
    _taskBgView = [[UIView alloc] initWithFrame:CGRectMake(0, _trackViewHeight, self.width, 0) ];
    _taskBgView.backgroundColor = kColor_bg;
    [self addSubview:_taskBgView];
    
    // 【4.9.7需求】去掉处理环节的挂起、跟进、积分、退回、协同等子任务
    if (![trackModel.trackCode isEqualToString:@"NODE_DS_DEAL"]) {
        [trackModel.tasks enumerateObjectsUsingBlock:^(SEGOrderTasks * model, NSUInteger idx, BOOL * _Nonnull stop) {
            //加急信息
            SEGOrderTasksView *taskView = [[SEGOrderTasksView alloc] initWithFrame:CGRectMake(0, taskBgViewHeight, _taskBgView.width, 0) taskModel:model];
            
            taskBgViewHeight += taskView.height;
            [_taskBgView addSubview:taskView];
        }];
    }
    
    //重新设置任务视图高度和轨迹视图高度
    _taskBgView.height = taskBgViewHeight;
    _trackViewHeight += taskBgViewHeight;
}


/**
 布局工单轨迹提单信息视图
 */
-(void)setUpTrackDetailInfoWith:(SEGOrderTrackInfoModel *)trackModel
{
    NSMutableArray *dataArraty = [[NSMutableArray alloc] init];
    //只解析照片，录音，文本三种类型控件
    [trackModel.variables enumerateObjectsUsingBlock:^(SEGTemplateModel * model, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([model.mDictionary[@"widgetType"] isEqualToString:@"text"] || [model.mDictionary[@"widgetType"] isEqualToString:@"file"]  || [model.mDictionary[@"widgetType"] isEqualToString:@"record"]) {
            if ([model.mDictionary[@"attrValue"] length] > 0) {
                [dataArraty addObject:model.mDictionary];
            }
        }
        else if ([model.mDictionary[@"widgetType"] isEqualToString:@"photo"]) {
            NSArray* picArray = [self arrayWithJsonString:model.mDictionary[@"attrValue"]];
            if (picArray.count > 0) {
                [dataArraty addObject:model.mDictionary];
            }
        }
    }];
    if (dataArraty.count == 0) {
        return;
    }
   /* 处理信息*/
    UIView *trackDetailView = [[UIView alloc] initWithFrame:CGRectMake(0, _trackViewHeight, self.width, 0)];
    trackDetailView.backgroundColor = kColor_W;
    [self addSubview:trackDetailView];
    //记录提单信息视图高度
    __block CGFloat trackDetailViewHeight = 0;
    /* 竖线*/
    UIView *vLineView = [[UIView alloc] initWithFrame:CGRectMake(kVlineViewX, 0, 1, 0)];
    vLineView.backgroundColor = kColor_L2;
    [trackDetailView addSubview:vLineView];
    //渲染
    [dataArraty enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL * _Nonnull stop) {
        CGFloat taskViewWidth = self.frame.size.width-kVlineViewX-30;
        if ([dict[@"widgetType"] isEqualToString:@"text"]) {
            SEGTrackTextView *textView = [[SEGTrackTextView alloc] initWithFrame:CGRectMake(kVlineViewX+15, trackDetailViewHeight, taskViewWidth, 0) dict:dict];
            [trackDetailView addSubview:textView];
            trackDetailViewHeight += textView.height;
            
        } else if ([dict[@"widgetType"] isEqualToString:@"file"]) {
            SEGTrackFileView *textView = [[SEGTrackFileView alloc] initWithFrame:CGRectMake(kVlineViewX+15, trackDetailViewHeight, taskViewWidth, 0) dict:dict];
            [trackDetailView addSubview:textView];
            trackDetailViewHeight += textView.height;
            
        } else if ([dict[@"widgetType"] isEqualToString:@"photo"]) {
            SEGTrackPhotoView *photoView = [[SEGTrackPhotoView alloc] initWithFrame:CGRectMake(kVlineViewX+15, trackDetailViewHeight, taskViewWidth, 0) data:dict];
            [trackDetailView addSubview:photoView];
            photoView.parentVC = self.parentVC;
            trackDetailViewHeight += photoView.height;
            
        } else if ([dict[@"widgetType"] isEqualToString:@"record"]) {
           SEGTrackRecordView *recordView = [[SEGTrackRecordView alloc] initWithFrame:CGRectMake(kVlineViewX+15, trackDetailViewHeight, taskViewWidth, 0) data:dict];
            [trackDetailView addSubview:recordView];
            trackDetailViewHeight += recordView.height;
        }
        
    }];
    
    //重设提单视图高度
    trackDetailView.height = trackDetailViewHeight;
    vLineView.height = trackDetailViewHeight;
    
    //重新计算轨迹视图高度
    _trackViewHeight += trackDetailViewHeight;
}


#pragma mark -json串转换成数组
- (id)arrayWithJsonString:(NSString *)jsonString{
    if (jsonString == nil) {
        return nil;
    }
    // 处理传过来的数据就是Array的情况
    if ([jsonString isKindOfClass:[NSArray class]]) {
        return jsonString;
    }
    
    if (![jsonString isKindOfClass:[NSString class]]) {
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSArray *arr = [NSJSONSerialization JSONObjectWithData:jsonData
                                                   options:NSJSONReadingMutableContainers
                                                     error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return arr;
}

/**
 配置环节描述信息
 */
-(NSArray *)configTrackDescInfoWith:(SEGOrderTrackInfoModel *)trackModel
{
    //    NODE_DS_SUBMIT：提单；
    //    NODE_DS_DISPATCH：派单；
    //    NODE_DS_TAKE：接单；
    //    NODE_DS_DEAL：处理；
    //    NODE_DS_CHECK：审核；
    //    NODE_DS_EVLUATE：评价；
    //    NODE_DS_RECALL：回访；
    //    NODE_DS_USER_DEFINRED：自定义
    //    NODE_DS_OVER:结束

    NSString *trackDescStr = @"";
    //环节描述信息规则 - 获取一些公共参数
    NSString *trackCodeStr = trackModel.trackCode;//环节编码
    NSString *trackName = trackModel.trackName;//当前环节名称
    _currUserName = @"";
    _currFirstUserName = @"";
    _nextUserName = @"";
    _nextFirstUserName = @"";
    if (trackModel.currentUser.count > 0) {
      _currFirstUserName = [trackModel.currentUser[0] valueForKey:@"userName"];//当前处理人名字第一个
      _currUserName = [self getCurrUserName:trackModel.currentUser];//当前处理人名字拼接
    }
    NSString *_nextFirstUserName = @"";
    if (trackModel.nextUser.count > 0) {
        _nextFirstUserName = [trackModel.nextUser[0] valueForKey:@"userName"];//下一步处理人名字第一个
        _nextUserName = [self getCurrUserName:trackModel.nextUser];//下一步处理人名字拼接
    }
    NSString *trackStatus = trackModel.trackStatus;//环节状态
    NSString *name = @"";
    if(trackModel.currentUser && trackModel.currentUser.count > 0) {
        name = trackModel.currentUser[0].userName;
    }
    if ([trackCodeStr isEqualToString:@"NODE_DS_SUBMIT"]) {//提交
        trackDescStr = @"客户提交了服务申请";
    } else if ([trackCodeStr isEqualToString:@"NODE_DS_DISPATCH"]) {//派单
        if([trackStatus isEqualToString:@"0"]) {
            trackDescStr = @"正在派单中";
        } else {
            trackDescStr = @"已派单";
        }
    } else if ([trackCodeStr isEqualToString:@"NODE_DS_TAKE"]) {//接单
        if([trackStatus isEqualToString:@"0"]) {
            trackDescStr = @"正在接单中";
        } else {
            trackDescStr = @"已接单";
        }
    } else if ([trackCodeStr isEqualToString:@"NODE_DS_DEAL"]) {//处理
        if([trackStatus isEqualToString:@"0"]) {
            trackDescStr = @"正在处理中";
        } else {
            trackDescStr = @"处理完成";
        }
    } else if ([trackCodeStr isEqualToString:@"NODE_DS_EVLUATE"]) {//评价
        if([trackStatus isEqualToString:@"0"]) {
            trackDescStr = @"客户正在评价中";
        } else {
            //判断是否是系统评价的
            if ([trackModel.opUserType isEqualToString:@"2"]) {
                trackDescStr = [NSString stringWithFormat:@"客户超时未评价，系统默认评价为%@星",trackModel.evalue];
            }
            else {
                trackDescStr = [NSString stringWithFormat:@"客户已评价，评价%@星", trackModel.evalue];
            }
        }
    } else if ([trackCodeStr isEqualToString:@"NODE_DS_PAY"] || [trackCodeStr isEqualToString:@"NODE_DS_PAID_NEW"]) {//支付
        if([trackStatus isEqualToString:@"0"]) {
            trackDescStr = @"支付中";
        } else {
            trackDescStr = @"支付成功";
        }
    } else if ([trackCodeStr isEqualToString:@"NODE_DS_RECALL"]) {//回访
        if([trackStatus isEqualToString:@"0"]) {
            trackDescStr = [NSString stringWithFormat:@"%@中", trackName];
        } else {
            NSMutableString *recallStr = [NSMutableString stringWithFormat:@"已%@，", trackModel.trackName];
            if (![trackModel.opUserType isEqualToString:@"2"] && trackModel.evalue.floatValue > 0)  {
                [recallStr appendString:[NSString stringWithFormat:@"经过%@协商，评价修改为%@星，", trackModel.trackName, trackModel.evalue]];
            }
            if ([trackModel.isAgree isEqualToString:@"1"]) {
                trackDescStr =[NSString stringWithFormat:@"%@%@通过",recallStr, trackModel.trackName];
            } else {
                trackDescStr = [NSString stringWithFormat:@"%@%@不通过",recallStr, trackModel.trackName];
            }
        }
        
    } else if ([trackCodeStr isEqualToString:@"NODE_DS_CHECK"]) {//审核
        if([trackStatus isEqualToString:@"0"]) {
            trackDescStr = @"审核中";
        } else {
            if ([trackModel.isAgree isEqualToString:@"1"]) {
                trackDescStr = @"审核通过";
            } else {
                trackDescStr = @"审核不通过";
            }
        }
    } else if ([trackCodeStr isEqualToString:@"NODE_DS_OVER"] || [trackCodeStr isEqualToString:@"NODE_DS_CANCEL"] || [trackCodeStr isEqualToString:@"NODE_DS_CLOSE"]) {//结束
        trackDescStr = @"工单已结束";
        if ([trackCodeStr isEqualToString:@"NODE_DS_CANCEL"] && trackModel.currentUser && trackModel.currentUser.count > 0 && trackModel.currentUser.firstObject.userType.integerValue == 1) {
            trackDescStr = [NSString stringWithFormat:@"客户撤销了工单，撤销理由：%@", trackModel.dealAdvise];
        }
    } else {//包含自定义环节
        
        if([trackStatus isEqualToString:@"0"]) {
            trackDescStr = [NSString stringWithFormat:@"%@中",trackName];
        } else {
            // 需求7350 其余环节：如果操作是用户，则显示接口返回的userName，其余固定展示"物业"
            trackDescStr = [NSString stringWithFormat:@"%@已%@",_currUserName ,trackName];
        }
    }

    return @[trackDescStr];
}

/**
 适配需要有展开按钮的富文本
 */
-(void)fitAttributedString:(NSString*)text lineSpace:(CGFloat)lineSpace font:(UIFont *)font label:(UILabel *)label
{
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
    [paragraphStyle setLineSpacing:lineSpace];
  
    //设置部分字体颜色
    NSDictionary *dictAttr1 = @{NSForegroundColorAttributeName:kColor_1,NSFontAttributeName:font,NSParagraphStyleAttributeName:paragraphStyle,NSFontAttributeName:font};
    NSDictionary *dictAttr2 = @{NSForegroundColorAttributeName:kColor_S3,NSFontAttributeName:font,NSParagraphStyleAttributeName:paragraphStyle,NSFontAttributeName:font};
    
    NSString *descStr = [text substringWithRange:NSMakeRange(0, text.length-2)];
    NSString *openStr = [text substringWithRange:NSMakeRange(text.length-2, 2)];
    
    NSMutableAttributedString *muAttrStr = [[NSMutableAttributedString alloc] initWithString:descStr attributes:dictAttr1];
    NSAttributedString *attributedStr = [[NSAttributedString alloc] initWithString:openStr attributes:dictAttr2];
    [muAttrStr appendAttributedString:attributedStr];
    
    label.attributedText = muAttrStr;
  
}


-(NSString *)getCurrUserName:(NSArray *)userArray
{
    // 4.9.6 7350需求 和安卓逻辑保持一致，只取第一个Model，其他情况都返回“物业”
    __block NSString *currUser = @"物业";
    // 1.客户 2.员工 3.系统
    if (userArray.count > 0) {
        SEGOrderCurrentUser *currentUser = userArray.firstObject;
        if (currentUser.userType.integerValue == 1) {
            return ValueToString(currentUser.userName);
        }
    }
    return currUser;
    
//    [userArray enumerateObjectsUsingBlock:^(SEGOrderCurrentUser * _Nonnull userModel, NSUInteger idx, BOOL * _Nonnull stop) {
//        if (userArray.count > 1  ) {
//            if (userModel.userName.length>0) {
//                if (idx == 0) {
//                    currUser = userModel.userName;
//                }
//                else{
//                    currUser = [currUser stringByAppendingString:[NSString stringWithFormat:@",%@",userModel.userName]];
//                }
//
//            }
//        }
//        else{
//            currUser = userModel.userName;
//        }
//    }];
//    return currUser;
}



@end
